package cn.zyjblogs.server.user.controller;

import cn.zyjblogs.server.user.dto.UserDto;
import cn.zyjblogs.server.user.dto.UserPageDto;
import cn.zyjblogs.server.user.po.UserPo;
import cn.zyjblogs.server.user.service.UserService;
import cn.zyjblogs.server.user.vo.UserVo;
import cn.zyjblogs.starter.common.entity.context.BaseContext;
import cn.zyjblogs.starter.common.entity.response.HttpCode;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.common.utils.bean.BeanUtils;
import cn.zyjblogs.starter.web.apiversion.ApiVersion;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhuyijun
 */
@Api(tags = {"用户管理"})
@RestController
@RequestMapping("/{v}/user")
@RequiredArgsConstructor
@ResponseBody
@Log4j2
@ApiVersion(1)
public class UserController {
    private final UserService userService;


    /**
     * 通过id查询用户信息
     *
     * @param id
     * @return cn.zyjblogs.rbac.server.user.po.UserPo
     * @author zhuyijun
     * @date 2022/9/19 上午12:06
     */
    @ApiOperation(value = "", notes = "通过id查询用户")
    @GetMapping("/id")
    @ApiVersion(1)
    public ResponseObject<UserVo> findById(@RequestParam String id) {
        UserPo userPo = userService.getById(id);
        return ResponseResult.success(BeanUtils.map(userPo, UserVo.class));
    }

    @ApiOperation(value = "", notes = "通过id查询用户")
    @GetMapping("/findCurrentUser")
    @ApiVersion(1)
    public ResponseObject<UserVo> findCurrentUser() {
        UserPo userPo = userService.getById(BaseContext.getUserId());
        return ResponseResult.success(BeanUtils.map(userPo, UserVo.class));
    }

    /**
     * @param userPageDto
     * @return cn.zyjblogs.starter.common.entity.response.ResponseObject<com.baomidou.mybatisplus.extension.plugins.pagination.Page < cn.zyjblogs.rbac.server.user.po.UserPo>>
     * @author zhuyijun
     * @date 2022/9/19 上午1:14
     */
    @ApiOperation(value = "", notes = "分页查询")
    @GetMapping("/page")
    @ApiVersion(1)
    public ResponseObject<List<UserVo>> findPage(UserPageDto userPageDto) {
        IPage<UserVo> page = userService.findPage(userPageDto);
        return ResponseResult.success(page.getTotal(), page.getRecords());
    }

    /**
     * 通过id集合查询
     *
     * @param ids
     * @return cn.zyjblogs.starter.common.entity.response.ResponseObject<com.baomidou.mybatisplus.extension.plugins.pagination.Page < cn.zyjblogs.rbac.server.user.po.UserPo>>
     * @author zhuyijun
     * @date 2022/9/19 上午1:14
     */
    @ApiOperation(value = "", notes = "通过id集合查询用户")
    @PostMapping("/findByIds")
    @ApiVersion(1)
    public ResponseObject<List<UserVo>> findByIds(List<String> ids) {
        return ResponseResult.success(userService.findByIds(ids));
    }

    @ApiOperation(value = "", notes = "用户保存")
    @PutMapping("/saveUser")
    @ApiVersion(1)
    public ResponseObject<String> saveUser(@RequestBody @Validated UserDto userDto) {
        if (userService.checkUser(userDto)) {
            return ResponseResult.error(HttpCode.BAD_REQUEST, "该账号已存在");
        }
        return userService.saveUser(userDto);
    }
}
